
extends Node

var utils = null
var ctrl = null
var diff
var rightBtn
var leftBtn
var base = null
var path = null
var candodge:bool = false
var window
var rndMode
var recordWindow
var shop
var charaBuildBonus = 0
var shieldBonus = 1
var endlessopt:bool = false
var a
var btn
var t = []
var c
func _ready():
	pass
func _init():
	call_deferred("init")
func _connect():
	pass
	
func init():
	yield(sys.get_tree().create_timer(0.2), "timeout")
	if globalData.infoDs.has("g_AneHekBase"):
		base = globalData.infoDs["g_AneHekBase"]
		path = chaData.infoDs["cTDW"].dir
	if base != null:
		#初始化
		utils = globalData.infoDs["g_AneHekUtils"]
		ctrl = globalData.infoDs["g_AneHek"]
		base.connect("onSaveGame",self,"backTitle")
		build()
		#loadGuide()
#加载主界面图片
func loadGuide():
	#if btn != null:
	#	btn.queue_free()
	btn = TextureButton.new()
	for i in range(8):
		var it = base.loadImg(path,"test/btn/"+ str(i) + ".png")
		t.append(it)
	btn.set_size(Vector2(370,140))
	btn.set_position(Vector2(630,600))
	btn.connect("pressed",self,"openit")
	#if c != null:
	#	c.queue_free()
	c = TextureRect.new()
	a = sys.get_node("/root/Control")
	a.add_child(c)
	c.set_texture(base.loadImg(path,"test/btn/kuang.png"))
	c.set_size(Vector2(72,63))
	c.set_position(Vector2(800,590))
	if !candodge:
		#if c != null:
		#	c.queue_free()
		for i in range(8):
			var tmp = TextureRect.new()
			tmp.set_texture(t[i])
			tmp.set_size(Vector2(370,140))
			tmp.set_position(Vector2(630,600))
			a.add_child(tmp)
	a.add_child(btn)
#	a.call_deferred("add_child",self)
#新建窗口
func build():
	#	print(1)
	#if window != null:
	#	window.queue_free()
	var tmp = utils.cBtns(sys.get_node("/root/Control/Panel2/Panel"), self, "塔防之光", "openit", [], Vector2(100, 700))
	yield(sys.get_tree().create_timer(0.2), "timeout")
	window = WindowDialog.new()
	window.set_size(Vector2(400,600))
	window.window_title = "设置"
	sys.get_node("/root/Control").add_child(window)

	var statisScroll = ScrollContainer.new()
	statisScroll.name = "ScrollContainer"
	statisScroll.set_custom_minimum_size(Vector2(300, 580))
	statisScroll.rect_position = Vector2(100, 50)
	statisScroll.set_margin(1, 50)
	window.add_child(statisScroll)

	var vbox = VBoxContainer.new()
	statisScroll.add_child(vbox)

	#MOD说明
	#utils.cBtn("guide", vbox, self)
	#buildHelp()
	utils.cBtn("Log", vbox, self)

	#图标闪烁设置
	#var guide = CheckButton.new()
	#guide.text = "图标闪烁"
	#guide.connect("pressed", self, "switchdodge", [guide])
	#vbox.add_child(guide)
	#if base.getSetting("candodge", true):
	#	guide.pressed = true
	#	candodge = true

	#红包活动设置
	#var red = CheckButton.new()
	#red.text = "红包活动"
	#red.connect("pressed", self, "switchRed", [red])
	#vbox.add_child(red)
	#if base.getSetting("RedPacket", true):
	#	red.pressed = true

	#全局词条
	var servo = CheckButton.new()
	servo.text = "全局词条[待完成]"
	servo.connect("pressed", self, "switchServo", [servo])
	vbox.add_child(servo)
	if base.getSetting("servotMode", true):
		servo.pressed = true
	
	var expert = CheckButton.new()
	#普通模式设置
	var normal = CheckButton.new()
	normal.text = "普通模式"
	normal.connect("pressed",self,"switchNormal",[normal,expert])
	vbox.add_child(normal)
	if base.getSetting("normalMode",true):
		expert.pressed = false
		normal.pressed = true

	#挑战模式设置
	expert.text = "专家模式"
	expert.connect("pressed", self, "switchExpert", [expert,normal])
	vbox.add_child(expert)
	if base.getSetting("expertMode", false):
		expert.pressed = true
		normal.pressed = false

	#娱乐模式设置
	#rndMode = CheckButton.new()
	#rndMode.text = "娱乐模式"
	#rndMode.connect("pressed", self, "switchRndMode", [rndMode])
	#vbox.add_child(rndMode)
	#if base.getSetting("rndMode", false):
	#	rndMode.pressed = true
	#	rndMode.text += ":" + base.getSetting("rndChoose","火系")

	#无尽EX
	#var exBtn = CheckButton.new()
	#exBtn.text = "真·无尽模式"
	#exBtn.connect("pressed", self, "switchEx", [exBtn])
	#vbox.add_child(exBtn)
	#if base.getSetting("enableEx", true):
	#	exBtn.pressed = true

	#最大天赋数量
	#var hbox = HBoxContainer.new()
	#utils.createLabel("最大天赋数：", hbox)
	#var talents = OptionButton.new()
	#talents.add_item("8", 8)
	#talents.add_item("9", 9)
	#talents.add_item("10", 10)
	#talents.add_item("11", 11)
	#talents.add_item("12", 12)
	#talents.add_item("13", 13)
	#talents.add_item("14", 14)
	#talents.add_item("15", 15)
	#talents.add_item("16", 16)
	#hbox.add_child(talents)
	#vbox.add_child(hbox)
	#talents.connect("item_selected", self, "switchTalents", [talents])
	#var talentSetting = base.getSetting("talentSetting", {"index":0,"size":8})
	#talents.select(talentSetting.index)
	#难度
	var diffBox = HBoxContainer.new()
	vbox.add_child(diffBox)


	var maxDiff = int(base.getSetting("maxDiff", 1))
	var diff = base.getSetting("difficult", 1)
	if maxDiff == null:
		maxDiff = 8 #改成从8
		diff = base.getSetting("difficult", 1)
	base.setSetting("maxDiff", maxDiff)
	leftBtn = TextureButton.new()
	leftBtn.set_normal_texture(base.loadImg(path, "g_AneHekModconfig/zuo.png"))
	leftBtn.set_disabled_texture(base.loadImg(path, "g_AneHekModconfig/zuoan.png"))
	diffBox.add_child(leftBtn)
	var diffLabel = utils.cLb("Mod难度：%d"%diff, diffBox)
	rightBtn = TextureButton.new()
	rightBtn.set_normal_texture(base.loadImg(path, "g_AneHekModconfig/you.png"))
	rightBtn.set_disabled_texture(base.loadImg(path, "g_AneHekModconfig/youan.png"))
	diffBox.add_child(rightBtn)
	leftBtn.disabled = diff == 0
	rightBtn.disabled = diff == maxDiff + 2

	leftBtn.connect("pressed",self,"switchDiff", [leftBtn, "sub", diffLabel])
	rightBtn.connect("pressed",self,"switchDiff", [rightBtn, "plus", diffLabel])
#红包活动
func switchRed(btn):
	base.setSetting("RedPacket",btn.pressed)
	if btn.pressed:
		sys.newBaseMsg("提示","红包活动是指战斗结束后，概率遇到红包雨，红包可开奖品；红包大概率正向奖励，小概率反向奖励")
#返回标题
func backTitle():
	#yield(sys.get_tree().create_timer(0.2), "timeout")
	#print(1)
	build()
	#loadGuide()
#切换是否闪烁
func switchdodge(btn):
	base.setSetting("candodge",btn.pressed)
	candodge = btn.pressed
#普通模式
func switchNormal(btn,a):
	base.setSetting("normalMode", btn.pressed)
	if btn.pressed:
		sys.newBaseMsg("提醒", "普通模式下，血量为400，每回合回血20，开局额外赠送1件装备")
		a.pressed = false
		switchExpert(a,null)
#专家模式
func switchExpert(btn,a):
	base.setSetting("expertMode", btn.pressed)
	if btn.pressed:
		sys.newBaseMsg("提醒", "专家模式下，血量为100，每回合不回血，开局额外赠送2件装备")
		a.pressed = false
		switchNormal(a,null)
#全局词条
func switchServo(btn):
	base.setSetting("servoMode", btn.pressed)
	if btn.pressed:
		sys.newBaseMsg("提醒", "全局词条是指开局和每打完一次boss可以从3个词条中选择一条作为全局加成")
#娱乐模式
func switchRndMode(btn):
	base.setSetting("rndMode", btn.pressed)
	if btn.pressed:
		sys.newBaseMsg("提醒", "娱乐模式下，整局游戏只会获得一个系列的英雄，纯粹娱乐，难度较低。")
		yield(sys.get_tree().create_timer(0.2), "timeout")
		var names = ["火系","水系","风系","光系","暗系"]
		var w = WindowDialog.new()
		w.set_size(Vector2(500,100))
		w.window_title = "选择系列"
		window.add_child(w)
		for i in range(5):
			utils.cBtns(w,self,names[i],"switchRndChoose",[names[i],w],Vector2(25 + i * 100,25))
		w.popup_centered()
	else:
		rndMode.text = "娱乐模式"
#娱乐模式选择系列
func switchRndChoose(name,w):
	base.setSetting("rndChoose",name)
	sys.newBaseMsg("提示","成功选择" + name)
	rndMode.text += ":" + base.getSetting("rndChoose","火系")
	w.hide()
#切换难度
func switchDiff(btn, action, diffLabel):
	var maxDiff = int(base.getSetting("maxDiff", 8))
	var diff = int(base.getSetting("difficult", 1))
	if action == "plus":
		diff += 1
	else:
		diff -= 1
	leftBtn.disabled = diff == 0
	rightBtn.disabled = diff == maxDiff + 2

	diffLabel.text = "Mod难度：%d"%diff
	base.setSetting("difficult", diff)
#更新日志
func log():
	var helpPop = WindowDialog.new()
	helpPop.window_title = "更新日志"
	helpPop.set_size(Vector2(1100,500))
	window.add_child(helpPop)
	var textBox = utils.cRLb(utils.readChangeLog(), helpPop, Vector2(1000,400))
	textBox.margin_top = 50
	textBox.margin_left = 50
	helpPop.popup_centered()
#mod指引
func guide():
	var guidePop = WindowDialog.new()
	guidePop.window_title = "Mod指引"
	guidePop.set_size(Vector2(1100,500))
	window.add_child(guidePop)
	var textBox = utils.cRLb(utils.readGuideLog(), guidePop, Vector2(1000,400))
	textBox.margin_top = 50
	textBox.margin_left = 50
	guidePop.popup_centered()
#弹出设置窗口
func openit():
	window.popup_centered()